package com.lsmo.android.carfans.activity.news;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lsmo.android.carfans.R;
import com.lsmo.android.carfans.app.view.NewsScrollView;

import android.app.Activity;
import android.app.ListActivity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class NewsActivity extends Activity {
	private ViewPager newsViewPager;
	private TextView topNewsTitile;

	private Drawable news_main_background;
	private MyPagerAdapter myAdapter;
	private static View viewTemp;


	private LayoutInflater mInflater;
	private List<View> mListViews;
	private View layout1 = null;
	private View layout2 = null;
	private View layout3 = null;
	private ImageView pic1, pic2, pic3;// 顶部新闻图片
	private ImageView[] topNews;
	private int lastIndex = 0;// 上一个图片的数组下标
	private LinearLayout newsContainer;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.news_main_layout);

		initView();
	}

	private void initView() {
		myAdapter = new MyPagerAdapter();
		newsViewPager = (ViewPager) findViewById(R.id.newsViewPager);
		newsViewPager.setAdapter(myAdapter);

		mListViews = new ArrayList<View>();
		mInflater = getLayoutInflater();

		layout1 = mInflater.inflate(R.layout.news_main_layout_viewimg1, null);
		layout2 = mInflater.inflate(R.layout.news_main_layout_viewimg2, null);
		layout3 = mInflater.inflate(R.layout.news_main_layout_viewimg3, null);
		pic1 = (ImageView) findViewById(R.id.news_main_top_pic_flag1);
		pic2 = (ImageView) findViewById(R.id.news_main_top_pic_flag2);
		pic3 = (ImageView) findViewById(R.id.news_main_top_pic_flag3);
		topNews = new ImageView[] { pic1, pic2, pic3 };
		for (int i = 0; i < topNews.length; i++) {
			topNews[i].setImageResource(R.drawable.news_main_unselectedflag);
		}
		mListViews.add(layout1);
		mListViews.add(layout2);
		mListViews.add(layout3);

		newsContainer = (LinearLayout) findViewById(R.id.news_main_layout_newsContainer);
		topNewsTitile=(TextView)findViewById(R.id.news_main_layout_topnews_txt);
		topNewsTitile.setText("敞篷跑车不要钱");
		topNews[0].setImageResource(R.drawable.news_main_selectedflag);
		news_main_background = newsContainer.getBackground();
		NewsAdapter adapter = new NewsAdapter(this, newsContainer,
				new TestNewsData().getNewsData());
		// 初始化当前显示的view
		newsViewPager.setCurrentItem(0);
		
		newsViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			public void onPageSelected(int arg0) {
				topNews[lastIndex].setImageResource(R.drawable.news_main_unselectedflag);
				topNews[arg0].setImageResource(R.drawable.news_main_selectedflag);
				if(arg0==0)
					topNewsTitile.setText("敞篷跑车不要钱");
				if(arg0==1)
					topNewsTitile.setText("悍马越野车也不要钱");
				if(arg0==2)
					topNewsTitile.setText("这个小红车自然也不要钱");
				lastIndex = arg0;
			}

			public void onPageScrolled(int arg0, float arg1, int arg2) {
			
			}

			public void onPageScrollStateChanged(int arg0) {
		

			}
		});

	}

	@Override
	protected void onResume() {
		super.onResume();
		if (viewTemp != null) {
			viewTemp.setBackgroundDrawable(news_main_background);
		
		}
	}

	private class MyPagerAdapter extends PagerAdapter {

		public void destroyItem(View arg0, int arg1, Object arg2) {
			Log.e("sc", "destroyItem");
			((ViewPager) arg0).removeView(mListViews.get(arg1));
		}

		public void finishUpdate(View arg0) {
		}

		public int getCount() {
			return mListViews.size();
		}

		public Object instantiateItem(View arg0, int arg1) {
			Log.e("sc", "instantiateItem");
			((ViewPager) arg0).addView(mListViews.get(arg1), 0);
			return mListViews.get(arg1);
		}

		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == (arg1);
		}

		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		public Parcelable saveState() {
			return null;
		}

		public void startUpdate(View arg0) {
		}

	}

	public static View getViewTemp() {
		return viewTemp;
	}

	public static void setViewTemp(View viewTemp) {
		NewsActivity.viewTemp = viewTemp;
	}
}
